<?php

    namespace Logistics\Controller;

    use Common\Controller\AdminbaseController;

    class AdminCarController extends AdminbaseController {

        protected $m_car,$m_driver,$m_card,$m_logistics;

        function _initialize() {
            parent::_initialize();
            $this->m_car= D("Logistics/Car");
            $this->m_driver= D("Logistics/Driver");
            $this->m_card = D("Admin/Card");
            $this->m_logistics = M("Logistics");
        }

        /**
         * 驾驶员管理
         */
        public function index(){

//            $s_rid = I("post.s_rid");
//            $this->assign("s_rid",$s_rid);
//            if(!empty($s_rid)){
//                $where['r.rid'] =$s_rid;
//            }else{
//
            //$where = array();
//            }
            if($this->user_model == 4){
                $where = array();
            }else{
                $where = array('c.lid'=>get_logistics_id());
            }

            $count=$this->m_car->alias("c")->where($where)->count();
            $page = $this->page($count, 15);
            $list = $this->m_car->get_list($where,"create_time DESC",$page->firstRow, $page->listRows);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 排序
         */
        public function listorders(){
            $status = parent::_listorders($this->m_car,'sort');
            if ($status) {
                $this->success("排序更新成功！");
            } else {
                $this->error("排序更新失败！");
            }
        }

        // 添加驾驶员
        public function add(){

            $id = I("get.id",0,'intval');
            if($this->user_model == 4){
                $logistics_list = $this->m_logistics->select();
                $this->assign("logistics_list",$logistics_list);
            }

            $g_lid = I("get.lid",0,'intval');


            if(!empty($id)){;
                $item =  $this->m_car->alias("c")->field("c.*,d.driver_name,ca.number AS card_number")->where(array('c.id'=>$id))->join("bdt_driver d ON d.id = c.driver_id")->join("bdt_card ca ON ca.id = c.card_id")->find();
                $this->assign("item",$item);
                $lid = $item["lid"];
            }

            if($this->user_model == 4 && session("ADMIN_ID") == 10){
                if(!empty($g_lid)){
                    $lid = $g_lid;
                }elseif(empty($id)){
                    $lid = $logistics_list[0]["id"];
                }
            }else{
                $lid = get_logistics_id();
            }

            $driver_list = $this->m_driver->where("lid=".$lid." AND (cid=0 OR cid=".$id.")")->select();
            $card_list = $this->m_card->where("lid=".$lid." AND (cid=0 OR cid=".$id.")")->select();
            $this->assign("lid",$lid);
            $this->assign("driver_list",$driver_list);
            $this->assign("card_list",$card_list);

            $this->display();
        }


        // 文章添加提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = I("post.");
                $driver_id = I("post.driver_id");
                $card_id = I("post.card_id");
                $driver = $this->m_driver->where(array("id"=>$driver_id))->find();
                if(!empty($driver['cid']) && $driver['cid'] != $id){
                    $this->error("您选择的驾驶员已绑定其他车辆！");exit;
                }

                $card = $this->m_card->where(array("id"=>$card_id))->find();
                if(!empty($card['cid'])  && $driver['cid'] != $id){
                    $this->error("您选择的会员卡已绑定其他车辆！");exit;
                }

                if($this->user_model == 4 && session("ADMIN_ID") == 10){
                    $insert['lid'] = $insert["lid"];
                    if($driver["lid"] != $insert["lid"]){
                        $this->error("您选择的驾驶员不属于该物流公司！！");exit;
                    }
                    if($card["lid"] != $insert["lid"]){
                        $this->error("您选择的会员卡不属于该物流公司！！");exit;
                    }
                }else{
                    $insert['lid'] = get_logistics_id();
                }

                if(empty($id)){
                    $insert['uid'] = session('ADMIN_ID');
                    $insert['create_time'] = time();
                    $result =$this->m_car->add($insert);
                }else{
                    $result = $this->m_car->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    if(empty($id)){
                        $car_id = $this->m_car->getLastInsID();
                    }else{
                        $car_id = $id;
                    }
                    $this->m_driver->where(array("cid"=>$car_id))->save(array("cid"=>0));
                    $this->m_card->where(array("cid"=>$car_id))->save(array("cid"=>0));
                    $this->m_driver->where(array("id"=>$driver_id))->save(array("cid"=>$car_id));
                    $this->m_card->where(array("id"=>$card_id))->save(array("cid"=>$car_id));
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        /**
         * 解除绑定
         */
        public function unbind(){
            if(isset($_GET['id'])) {
                $id = I("get.id", 0, 'intval');
                $car = $this->m_car->where(array("id"=>$id))->find();
                $driver_id = $car['driver_id'];
                $card_id = $car['card_id'];
                $result = $this->m_car->where(array("id"=>$id))->save(array('driver_id'=>0,'card_id'=>0));
                if($result){
                    $this->m_driver->where(array('id'=>$driver_id))->save(array('cid'=>0));
                    $this->m_card->where(array('id'=>$card_id))->save(array('cid'=>0));
                    $this->success("解绑成功！");
                }
            }else{
                $this->error("参数有误！");
            }
        }




        // 文章删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                $car = $this->m_car->where(array('id'=>$id))->find();
                if ($this->m_car->where(array('id'=>$id))->delete() !==false) {
                    $this->m_driver->where(array('id'=>$car["driver_id"]))->save(array('cid'=>0));
                    $this->m_card->where(array('id'=>$car["card_id"]))->save(array('cid'=>0));
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

    }